from django.db import models

# 项目类型选择
PROJECT_TYPES = (
    ('web', 'Web应用'),
    ('mobile', '移动应用'),
    ('api', 'API接口'),
    ('desktop', '桌面应用'),
    ('other', '其他类型'),
)

class ProductLine(models.Model):
    """产品线模型"""
    name = models.CharField(max_length=200, verbose_name='产品线名称')
    description = models.TextField(blank=True, null=True, verbose_name='产品线描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_active = models.BooleanField(default=True, verbose_name='是否活跃')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '产品线'
        verbose_name_plural = '产品线管理'
        ordering = ['-created_at']

class Project(models.Model):
    """项目模型"""
    name = models.CharField(max_length=200, verbose_name='项目名称')
    description = models.TextField(blank=True, null=True, verbose_name='项目描述')
    project_type = models.CharField(max_length=50, choices=PROJECT_TYPES, verbose_name='项目类型')
    product_line = models.ForeignKey(ProductLine, on_delete=models.SET_NULL, null=True, blank=True, related_name='projects', verbose_name='所属产品线')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_active = models.BooleanField(default=True, verbose_name='是否活跃')
    creator = models.CharField(max_length=100, null=True, blank=True, verbose_name='创建人')
    department = models.CharField(max_length=100, null=True, blank=True, verbose_name='所属部门')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '项目'
        verbose_name_plural = '项目管理'
        ordering = ['-created_at']

class Version(models.Model):
    """项目版本模型"""
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='versions', verbose_name='所属项目')
    version_number = models.CharField(max_length=50, verbose_name='版本号')
    description = models.TextField(blank=True, null=True, verbose_name='版本描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    release_date = models.DateField(null=True, blank=True, verbose_name='发布日期')
    is_current = models.BooleanField(default=False, verbose_name='是否当前版本')

    def __str__(self):
        return f'{self.project.name} - {self.version_number}'

    class Meta:
        verbose_name = '项目版本'
        verbose_name_plural = '项目版本管理'
        ordering = ['-created_at']
        unique_together = ('project', 'version_number')
